﻿using System;
using Microsoft.Practices.Unity;
using RG.Common;

namespace RG.Common.WPF.Dialogs
{
    public class PreviewDlgVM
    {
        public IUnityContainer Cfg { get; set; }

        [Dependency]
        public IWorkplace Workplace { get; set; } 

        [InjectionConstructor]
        public PreviewDlgVM(IUnityContainer cfg)
        {
            Cfg = cfg;
        }

        private Object child;
        public Object Child
        {
            get { return child; }
            set
            {
                child = value;
                SetChild(child);
            }
        }

        public object ChildView { get; private set; }

        public void SetChild(object presenter)
        {
            var type = typeof(PreviewDlgVM);

            var methodInfo = type.GetMethod("CreateFrame");

            var genericArguments = new[] { presenter.GetType() };
            var genericMethodInfo = methodInfo.MakeGenericMethod(genericArguments);

            ChildView = genericMethodInfo.Invoke(this, new[] { presenter });
        }

        public object CreateFrame<T>(T presenter)
        {
            var view = Cfg.Resolve<IView<T>>();
            view.Presenter = presenter;
            return view;
        }
    }
}
